package com.itlin.controller;


import com.itlin.bean.ShoppingCart;
import com.itlin.common.R;
import com.itlin.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService cartService;


    /**
     * 添加购物车
     * @param cart
     * @return
     */
    @PostMapping("/add")
    public R add(@RequestBody ShoppingCart cart){

        ShoppingCart shoppingCart = cartService.add(cart);

        return R.success(shoppingCart);
    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R list(){
        List<ShoppingCart> list = cartService.findList();
        return R.success(list);
    }


    @DeleteMapping("/clean")
    public R clean(){
        int row = cartService.clean();
        if(row >0){
            return R.success("清空购物车成功!");
        }else{
            return R.error("清空购物车失败!");
        }

    }
}

